package com.study.modules.sys.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.common.exception.ApiException;
import com.study.common.utils.PubFinalData;
import com.study.modules.sys.mapper.SysMenuMapper;
import com.study.modules.sys.model.entity.SysMenu;
import com.study.modules.sys.service.SysMenuService;
import com.study.modules.sys.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author Mr.Du
 * @Date 2021/10/16 10:49
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;
    @Autowired
    private SysRoleMenuService roleMenuService;

    @Override
    public SysMenu selectMenuById(Long id) {
        return menuMapper.selectMenuById(id);
    }

    @Override
    public Boolean insertMenu(SysMenu sysMenu,Long userId){
        sysMenu.setCreateId(userId);
        sysMenu.setCreateTime(new Date());
        menuMapper.insert(sysMenu);
        return true;
    }

    @Override
    public Boolean updateMenuById(SysMenu sysMenu, Long userId){
        sysMenu.setUpdateId(userId);
        sysMenu.setUpdateTime(new Date());
        menuMapper.updateById(sysMenu);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteMenu(Long menuId, Long userId) {
        SysMenu sysMenu = baseMapper.selectMenuById(menuId);
        if (sysMenu == null) throw new ApiException("菜单不存在");
        //删除role_menu表
        roleMenuService.deleteRoleMenuByMenuId(menuId);
        //删除menu表
        sysMenu.setDeleted(PubFinalData.DELETED);
        sysMenu.setUpdateId(userId);
        sysMenu.setUpdateTime(new Date());
        menuMapper.updateById(sysMenu);
        return true;
    }

}
